<?php

session_start();

include "dbconnect.php";

if(!isset($_SESSION['Username']))

{

    header("location:index.php");

}

else 

{

    if($_SESSION['Type']!=3)

    header("location:unauthorized.php");

	//echo isset($_SESSION['Username']);

}

$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);

if(!$con)

{

	die("Error:cannot connect to database ");

}

mysql_select_db($_DB_NAME,$con);

mysql_query("set @username='".$_SESSION['Username']."'");

$studentrs = mysql_query("select * from users u,students s,degree d where u.username=@username and u.username=s.username and s.Degree=d.degreeid");

if($studentrs == false )

{

    die(mysql_error() );

}

$row = mysql_fetch_assoc($studentrs);

//validation needs to be handled

if($_SERVER["REQUEST_METHOD"] == "POST")

{


    $fname=$_POST["fname"];

    $lname=$_POST["lname"];

    $degree=$_POST["degree"];

    $year=$_POST["year"];

    $gpa=$_POST["gpa"];

    $tepaic=$_POST["tepaic"];

    $program=$_POST["program"];

    $experience=$_POST["experience"];



    if(!preg_match("/^[0-9]{1}+.[0-9]{1,2}+$/",$gpa)||!preg_match("/^[a-zA-Z]+$/",$fname)||!preg_match("/^[a-zA-Z]+$/",$lname))

	{

    }

    else

    {

        $con=mysql_connect($dbHost,$dbUserAndName,$dbPass);

        if(!$con)

        {

            die('Error:cannot connect to database');

        }

        mysql_select_db($_DB_NAME,$con);

        mysql_query("set @username='".$_SESSION['Username']."'");

        $sql="UPDATE users SET Firstname='$fname', Lastname='$lname' WHERE username=@username";

        if (!mysql_query($sql,$con))

        {

            die('Error:cannot update users table');

        }

        mysql_query("SET @pdegree='".$degree."'");

        $degreer=mysql_query("select degreeid from degree where description=@pdegree");

        

        if($degreer == false)

        {

            die(mysql_error());

        }

        $degreerr = mysql_fetch_assoc($degreer);

        $usernam=$_SESSSION["Username"];

         mysql_query("SET @pdegreeid=".$degreerr["degreeid"]);

        $sql1="UPDATE students SET Program='$program', Degree=@pdegreeid, Year=$year, GPA=$gpa, TEPAIC='$tepaic', Experience='$experience' WHERE username=@username";

        

        if(!mysql_query($sql1,$con))

        {

            die(mysql_error());

        }

        header('location:profile.php');

    }

}

function clean_input($data)

{

    $data = trim($data);

    $data = stripslashes($data);

    $data = htmlspecialchars($data);

    return $data;

}

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Profile</title>

<script src="media/js/jquery.js" type="text/javascript"></script>

<script src="media/js/jquery.datatables.js" type="text/javascript"></script>

<script type="text/javascript" charset="utf-8">

function togglevisibility()

{

	var reftext=document.getElementById('edit').innerHTML;

	if(reftext == 'Edit')

    {

        document.getElementById('edit').innerHTML='Cancel';

    }

    else

		document.getElementById('edit').innerHTML='Edit';

    var e = document.getElementById('editprofile');

    var r=document.getElementById('readProfile');

    if(e.style.display == 'block')

    {

        e.style.display = 'none';

        r.style.display = 'block';

    }

    else

    {

        e.style.display = 'block';

        r.style.display = 'none';

    }

}

</script>



<script>

function show_fname_error(str)

{

    var xmlhttp;

    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()

    {

        if (xmlhttp.readyState==4 && xmlhttp.status==200)

        {

            document.getElementById("fname_error").innerHTML=xmlhttp.responseText;

        }

    }

    xmlhttp.open("GET","input_error_info.php?p=fname&q="+str,true);

    xmlhttp.send();

}

</script>



<script>

function show_lname_error(str)

{

    var xmlhttp;

    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()

    {

        if (xmlhttp.readyState==4 && xmlhttp.status==200)

        {

            document.getElementById("lname_error").innerHTML=xmlhttp.responseText;

        }

    }

    xmlhttp.open("GET","input_error_info.php?p=lname&q="+str,true);

    xmlhttp.send();

}

</script>



<script>

function show_gpa_error(str)

{

    var xmlhttp;

    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()

    {

        if (xmlhttp.readyState==4 && xmlhttp.status==200)

        {

            document.getElementById("gpa_error").innerHTML=xmlhttp.responseText;

        }

    }

    xmlhttp.open("GET","input_error_info.php?p=gpa&q="+str,true);

    xmlhttp.send();

}

</script>

<script>

function set(openingid)

{

    //var hashid=CryptoJS.SHA256(openingid);

    window.location.href = 'apply.php?openingid='+openingid

}

</script>	

<!--<script>

$('#semYear').change()(function(){

                            $.ajax({

                                   type: "POST",

                                   url: "yearSuggest.php",

                                   data: {Semester: 'SPRING',Year:20},

                                   success: function(yearparam){

                                            $("#semYear").autocomplete({source: yearparam.split(',')});

                                   },

                                   error: function(){alert("Error");}

                                   });

});

</script> -->

<style type="text/css">

@import "media/css/demo_table_jui.css";

@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";

</style>

<link href="CSS/Global.css" type="text/css" rel="stylesheet" />

<link href="CSS/Base.css" type="text/css" rel="stylesheet" />

<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />



</head>



<body style="min-width:1280px">



<div id="branding-bar" class="gateway">

<div class="bar">

<div class="wrapper">

<h1 class="campus"><a><img alt="Indiana University" height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>

</div>

</div>

</div>

<div class="grey" >

</div>

<center>

    <div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

        <ul>

           <li style="width:200px;" ><a href="applyview.php">Apply</a></li>
           <li style="width:200px;"><a href="profile.php">Profile</a></li>

        </ul>

    </div>

</center>

<span style="width:100px;margin-left:300px;">

    <button class="hyperbutton" onClick="location.href = 'logout.php'" >logout</button>

</span>

    <div class="footbody">




        <table>
		<tbody>
        <tr>

            <td>

                <label class="Header2">My Profile</label>

            </td>
            <td/>
            <td/>
			<td/>
            <td/>
            <td/>
            <td/>
            <td/>
            <td/>
            <td>

                <a id="edit" onClick="togglevisibility();" class="crimson-text">Edit</a>

            </td>

        </tr>
		</tbody>
        </table>





        <table class="readProfile" id="readProfile" cellpadding="2" cellspacing="3">

        <tbody>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

                <tr>

                    <td>

                        <label class="crimson-text" >First name</label>

                    </td>

                    <td>

                        <input type="text" disabled class="crimson-border"/ value="<?php echo $row["Firstname"];?>">

                    </td>

                    <td/>

                    <td>

                        <label class="crimson-text" >Last name</label>

                    </td>
                    


                    <td>

                        <input type="text" disabled class="crimson-border"/ value="<?php echo $row["Lastname"];?>">

                    </td>
                    
                     <td/>
                        <td>

                            <label class="crimson-text" >Degree</label>

                        </td>

                        <td>

                            <select disabled>

                            <option value="BS" <?php if($row["description"]=="BS") echo "selected";?>>BS </option>

                            <option value="MS" <?php if($row["description"]=="MS") echo "selected";?>>MS </option>

                            <option value="PHD" <?php if($row["description"]=="PHD") echo "selected";?>>PHD </option>

                            </select>

                        </td>


                </tr>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            </tr>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

            <tr/>

                <tr>

                    <tr>


                        <td>

                            <label class="crimson-text" >Year</label>

                        </td>

                        <td>

                        <select disabled>
						
                        <option value=""  <?php if($row["TEPAIC"]=="") echo "selected";?>> </option>
                         
                        <option value=1 <?php if($row["Year"]==1) echo "selected";?>>1</option>

                        <option value=2 <?php if($row["Year"]==2) echo "selected";?>>2</option>

                        <option value=3 <?php if($row["Year"]==3) echo "selected";?>>3</option>

                        <option value=4 <?php if($row["Year"]==4) echo "selected";?>>4</option>

                        <option value=5 <?php if($row["Year"]==5) echo "selected";?>>5</option>

                        <option value=6 <?php if($row["Year"]==6) echo "selected";?>>6</option>

                        <option value=7 <?php if($row["Year"]==7) echo "selected";?>>7</option>

                        <option value=8 <?php if($row["Year"]==8) echo "selected";?>>8</option>

                        <option value=9 <?php if($row["Year"]==9) echo "selected";?>>9</option>

                        </select>

                    </td>

					 <td/>
                    <td>

                        <label class="crimson-text" >GPA</label>

                    </td>

                    <td>

                        <input type="text" disabled class="crimson-border" value="<?php echo $row["GPA"];?>"/>

                    </td>

                    <td/>

                    <td>

                        <label class="crimson-text" >TEPAIC</label>

                    </td>

                    <td>

                        <select disabled>
                        
                        <option value=""  <?php if($row["TEPAIC"]=="") echo "selected";?>> </option>

                        <option value="C1"  <?php if($row["TEPAIC"]=="C1") echo "selected";?>>C1</option>

                        <option value="C2"  <?php if($row["TEPAIC"]=="C2") echo "selected";?>>C2</option>

                        <option value="C3"  <?php if($row["TEPAIC"]=="C3") echo "selected";?>>C3</option>

                        <option value="NC4" <?php if($row["TEPAIC"]=="NC4") echo "selected";?>>NC4</option>

                        <option value="NC5" <?php if($row["TEPAIC"]=="NC5") echo "selected";?>>NC5</option>

                        </select>

                    </td>

                </tr>

                <tr/>

            	<tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                </tr>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                    <tr>

                        <td>

                            <label class="crimson-text" >Program</label>

                        </td>

                        <td>

                            <select disabled>
                            
							 <option value="UnderGraduate" <?php if($row["Program"]=="UnderGraduate") echo "selected";?>>Under Graduate</option>
                            <option value="Graduate" <?php if($row["Program"]=="Graduate") echo "selected";?>>Graduate</option>

                            </select>

                        </td>

                        <td/>

                        <td>

                            <label class="crimson-text" >Experience</label>

                        </td>

                        <td colspan="2" rowspan="2">

                             <textarea  disabled class="crimson-border"  cols="2"  cols="2" rows="2" style="width:100%"><?php echo $row["Experience"];?></textarea>


                        </td>

                    </tr>

                    </tbody>

                </table>
				

                <form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" >

                <table class="readProfile" id="editprofile" style="display:none" cellpadding="2" cellspacing="3">



                <tbody>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr/>

                <tr>

                    <td>

                        <label class="crimson-text" >First name</label>

                    </td>

                    <td>

                        <input type="text" class="crimson-border" name="fname" id="fname" onBlur="show_fname_error(this.value)"/ value="<?php echo $row["Firstname"];?>">

                    </td>

                    <td>

                    <span id="fname_error"></span>

                    </td>

                    <td>

                        <label class="crimson-text" >Last name</label>

                    </td>

                    <td>

                        <input type="text" class="crimson-border" name="lname" id="lname" onBlur="show_lname_error(this.value)"/ value="<?php echo $row["Lastname"];?>">

                    </td>

                    <td>

                    <span id="lname_error"></span>

                    </td>
                    
                    <td>

                        <label class="crimson-text" >Degree</label>

                    </td>

                    <td>

                        <select name="degree">

                        <option value="BS" <?php if($row["description"]=="BS") echo "selected";?>>BS </option>

                        <option value="MS" <?php if($row["description"]=="MS") echo "selected";?>>MS </option>

                        <option value="PHD" <?php if($row["description"]=="PHD") echo "selected";?>>PHD </option>

                        </select>

                    </td>

                <td/>
                
              <tr/>
                <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    </tr>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>
                    
                    <tr>

                    <td>

                        <label class="crimson-text" >Year</label>

                    </td>

                    <td>

                        <select name="year">

                        <option value=1 <?php if($row["Year"]==1) echo "selected";?>>1</option>

                        <option value=2 <?php if($row["Year"]==2) echo "selected";?>>2</option>

                        <option value=3 <?php if($row["Year"]==3) echo "selected";?>>3</option>

                        <option value=4 <?php if($row["Year"]==4) echo "selected";?>>4</option>

                        <option value=5 <?php if($row["Year"]==5) echo "selected";?>>5</option>

                        <option value=6 <?php if($row["Year"]==6) echo "selected";?>>6</option>

                        <option value=7 <?php if($row["Year"]==7) echo "selected";?>>7</option>

                        <option value=8 <?php if($row["Year"]==8) echo "selected";?>>8</option>

                        <option value=9 <?php if($row["Year"]==9) echo "selected";?>>9</option>

                        </select>

                    </td>

                    <td />
            	
                    <td>

                        <label class="crimson-text" >GPA</label>

                    </td>

                    <td>

                        <input type="text" class="crimson-border" name="gpa" id="gpa" onblur=show_gpa_error(this.value) value="<?php echo $row["GPA"];?>"/>

                    </td>

                    <td>

                    <span id="gpa_error"></span>

                    </td>

                    <td>

                        <label class="crimson-text" >TEPAIC</label>

                    </td>

                    <td>

                        <select name="tepaic">
						
                         <option value=""  <?php if($row["TEPAIC"]=="") echo "selected";?>> </option>
                         
                        <option value="C1"  <?php if($row["TEPAIC"]=="C1") echo "selected";?>>C1</option>

                        <option value="C2"  <?php if($row["TEPAIC"]=="C2") echo "selected";?>>C2</option>

                        <option value="C3"  <?php if($row["TEPAIC"]=="C3") echo "selected";?>>C3</option>

                        <option value="NC4" <?php if($row["TEPAIC"]=="NC4") echo "selected";?>>NC4</option>

                        <option value="NC5" <?php if($row["TEPAIC"]=="NC5") echo "selected";?>>NC5</option>

                        </select>

                    </td>

                    </tr>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    </tr>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr>

                        <td>

                            <label class="crimson-text" >Program</label>

                        </td>

                        <td>

                            <select name="program">
                            
							 <option value="UnderGraduate" <?php if($row["Program"]=="UnderGraduate") echo "selected";?>>Under Graduate</option>
                            <option value="Graduate" <?php if($row["Program"]=="Graduate") echo "selected";?>>Graduate</option>


                            </select>

                        </td>

                        <td/>

                        <td>

                            <label class="crimson-text" >Experience</label>

                        </td>

                        <td colspan="2" rowspan="2">

                            <textarea  name="experience" class="crimson-border"  cols="2" rows="2" style="width:100%"><?php echo $row["Experience"];?></textarea>

                        </td>

                    </tr>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    </tr>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr/>

                    <tr>

                    <td>

                        <input type="submit" value="Update"/>

                    </td>

                    </tr>

               

                    </tbody>

                    </table>

                    </form>

       </div>

</body>

</html>